package tema6;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.ServletContext;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@WebServlet(name="AddAtributosServlet", urlPatterns={"/tema6/addAtributos","/tema6/addModificandoAtributos"})
public class AddAtributosServlet extends HttpServlet {
    private final String dirAnhadirAtributos = "/EjemploBasicoJavaEE/tema6/addAtributos";
    private final String dirModificarAtributos = "/EjemploBasicoJavaEE/tema6/addModificandoAtributos";
    
    protected void procesaPeticion(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        String lugar = "peticion";
        ServletContext contexto = request.getServletContext();
        RequestDispatcher muestraAtributosServlet = contexto.getNamedDispatcher("ListaAtributosServlet");
        if(request.getRequestURI().equals(dirAnhadirAtributos)) {
            lugar = request.getParameter("lugar");
            muestraAtributosServlet = contexto.getNamedDispatcher("ListaAtributosServlet");
        } else {
            if(request.getRequestURI().equals(dirModificarAtributos)){
                lugar = request.getParameter("lugarHidden");
                muestraAtributosServlet = contexto.getNamedDispatcher("GenerarFormularioAtributosServlet");
            }
        }
        String atributo = request.getParameter("atributo");
        String valor = request.getParameter("valor");
        
        if(atributo == null || valor == null || atributo.equals("") || lugar == null){
            Logger.getLogger(AddAtributosServlet.class.getName()).log(Level.SEVERE,
                    "Parametros del formulario erroneos", new Exception());
            //Reenviaremos el request a otro formulario de error
        } else {
            if(lugar.equals("peticion"))
                anhadeAtributoAPeticion(atributo,valor,request);
            else
                if(lugar.equals("sesion"))
                    anhadeAtributoASesion(atributo,valor,request.getSession());
                else
                    if(lugar.equals("contexto"))
                        anhadeAtributoAContexto(atributo,valor,contexto);
                    else
                        anhadeAtributoACookie(atributo,valor,response);
        }
        
        muestraAtributosServlet.forward(request,response);
    }   
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        procesaPeticion(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        procesaPeticion(request, response);
    }
    
    private void anhadeAtributoAPeticion(String atributo,String valor,HttpServletRequest request){
        request.setAttribute(atributo, valor);
    }
    
    private void anhadeAtributoASesion(String atributo, String valor, HttpSession sesion){
        sesion.setAttribute(atributo,valor);
    }
    
    private void anhadeAtributoAContexto(String atributo, String valor, ServletContext contexto){
        contexto.setAttribute(atributo,valor);
    }
    
    private void anhadeAtributoACookie(String atributo, String valor, HttpServletResponse response){
        response.addCookie(new Cookie(atributo, valor));
    }

}
